import {
  createStore
} from 'vuex'
import axios from 'axios'
import persist from 'vuex-persistedstate'
export default createStore({
  state: {
    objList: [],
    objList1: [],
    checkFlag: false, // 定义底部选中
    localValue: '',

  },
  mutations: {
    pushObj(state, value) { // 添加数据
      if (localStorage.getItem('vuex')) {
        state.objList = JSON.parse(localStorage.getItem('vuex').objList)
        state.objList1 = JSON.parse(localStorage.getItem('vuex').objList)
      } else {
        state.objList = value
        state.objList1 = value
      }
    },

    checkAll(state, value) { // 全选
      state.objList.data.cartData.forEach(item => {
        item.checked = value
      })
    },
    check(state, value) {
      state.objList.data.cartData.forEach(item => {
        if (item.id == value.id) {
          item.checked = !item.checked
        }
      })
      state.checkFlag = state.objList.data.cartData.every(item => item.checked)
    },
    removeAll(state, value) {
      state.objList.data.cartData = state.objList.data.cartData.filter(item => !item.checked)
    },
    local(state, value) {
      state.localValue = value
      console.log(state.localValue);
    },
    search(state, value) {
      state.objList.data.cartData = state.objList1.data.cartData.filter(item => item.name.includes(state.localValue))
    }
  },


  actions: {
    getList(state) {
      axios.get('data.json').then(res => {
        state.commit('pushObj', res)
      })
    }

  },
  plugins: [persist()]
})